package net.hound.site.video;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import net.cl.tools.HttpProxy;
import net.cl.utils.HttpUtil;
import net.cl.utils.StringUtil;
import net.hound.IHound;

public class Tudou implements IHound {

	private static final String INDEX_PREFIX = "http://v2.tudou.com/v?vn=02&st=1%2C2&it=";
	private String url;
	private static final String encoding = "GBK";

	public Tudou(String url) {
		this.url = url;
	}

	@Override
	public String getSource(String userAgent) {
		String html = HttpUtil.getResponseString(url, encoding);
		String vid = StringUtil.getMatch(",iid = (\\d{1,})", html);
		if (vid == null)
			return "media not found.";
		String u = INDEX_PREFIX + vid;
		HttpProxy p = new HttpProxy(u);
		p.userAgent(userAgent);
		try {
			html = p.get();
		} catch (IOException e) {
			return null;
		}
		Document doc = Jsoup.parse(html);
		Elements files = doc.select("f");
		if (files != null && files.size() > 0) {
			for (int i = 0; i < files.size(); i++) {
				String source = files.get(i).text();
				if (source != null)
					return files.get(i).text();
			}
		}
		return null;
	}

	@Override
	public String getInfo(String userAgent) {
		// TODO Auto-generated method stub
		return null;
	}

}
